package com.tuyan.aixiang.activity.web;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

import com.tencent.smtt.sdk.WebView;
import com.tuyan.aixiang.R;
import com.tuyan.aixiang.common.AppData;
import com.tuyan.aixiang.common.BaseActivity;
import com.tuyan.aixiang.common.BasePresenter;
import com.tuyan.aixiang.util.ToastUtil;
import com.tuyan.aixiang.util.WebUtil;

import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;

import anet.channel.util.StringUtils;
import butterknife.BindView;

/**
 * Webview
 */
public class WebActivity extends BaseActivity<BasePresenter> {

    @BindView(R.id.wv_main)
    WebView wvMain;

    private String url;

    @Override
    public void bindView() {
        super.bindView();
        immersion();
        Intent intent = getIntent();
        String u = intent.getStringExtra("url");
        if (StringUtils.isBlank(u)) {
            ToastUtil.makeShort("地址无效");
            finish();
        }
        this.url = u;
        this.initWebView();
        showLoading();


        wvMain.loadUrl(this.url);
    }

    public static void open(Context context, String url) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    private void initWebView() {
        WebUtil.configWebview(this, wvMain);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK && wvMain.canGoBack()) {
                wvMain.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        try {
            wvMain.stopLoading();
            wvMain.setTag(null);
            wvMain.removeAllViews();
            wvMain.clearHistory();
            wvMain.clearCache(true);
            wvMain.setWebViewClient(null);
            wvMain.setWebChromeClient(null);
            wvMain.destroy();
        } catch (Exception ignore) {
        }
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        WebUtil.onActivityResult(requestCode, resultCode, data);
    }
}